package com.binguo.thirdPay.zy;

import com.binguo.model.model.Customer;
import com.binguo.model.model.ShopZyParam;
import com.binguo.model.repository.ShopZyParamRepository;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import tk.mybatis.mapper.entity.Condition;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ZytkService {

    public static String cardType = "2";
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    private RestTemplate restTemplate = new RestTemplate();

    @Autowired
    ShopZyParamRepository shopZyParamRepository;

    public static void main(String[] args) throws Exception{
        ZytkService zytkService = new ZytkService();
//        zytkService.SignIn("http://265m342s59.qicp.vip/forwardRequestYK");
        zytkService.GetAccDBMoney("20170016","http://26a8459b89.wicp.vip/forwardRequestMZ");


        //26170
//        zytkService.GetAccDBMoney("180603", "http://265m342s59.qicp.vip/forwardRequestYK");
//        zytkService.GetAccInfoSpec("9673", "http://26a8459b89.wicp.vip/forwardRequestMZ");
//        zytkService.AntiPaymentSpec("3454");
//        zytkService.TestGetAccInfo("2697318823",29);
//        zytkService.AntiPayment("2697318823");
//        String result = zytkService.PaymentA("2697318823","1",29);
//        System.out.println(result);

        /*
       服务器IP
192.168.2.118
站点号    59  和   61
商户号  1001
操作员号   1001

         */

    }

    /**
     * 第三方签到认证
     */
    public Map<String, Object> SignIn(String shopurl){

        String url = shopurl + "?op=SignIn";

        String request = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                "  <soap12:Header>\n" +
                "    <SecurityHeader xmlns=\"http://www.hzsun.com/\">\n" +
                "      <ThirdType>" + 1 + "</ThirdType>\n" +
                "      <Secret1>" + 1 + "</Secret1>\n" +
                "      <Secret2>" + 1 + "</Secret2>\n" +
                "    </SecurityHeader>\n" +
                "  </soap12:Header>\n" +
                "  <soap12:Body>\n" +
                "    <SignIn xmlns=\"http://www.hzsun.com/\" />\n" +
                "  </soap12:Body>\n" +
                "</soap12:Envelope>";


        System.out.println(request);
        HttpHeaders DEFAULT_JSON_HEADER = new HttpHeaders();
        MediaType jsontype = MediaType.TEXT_XML;
        DEFAULT_JSON_HEADER.setContentType(jsontype);
        DEFAULT_JSON_HEADER.add("Accept", MediaType.TEXT_XML.toString());

        HttpEntity<String> entity = new HttpEntity<>(request, DEFAULT_JSON_HEADER);

        ResponseEntity<String> result = restTemplate.postForEntity(url, entity, String.class);

        /**
         * response
         * <?xml version="1.0" encoding="utf-8"?>
         * <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
         *     <soap:Body>
         *         <SignInResponse xmlns="http://www.hzsun.com/">
         *             <SignInResult>1</SignInResult>
         *             <stanum>45</stanum>
         *             <ThirdType>306</ThirdType>
         *             <Secret1>2067533</Secret1>
         *             <Secret2>7b1sxwFNuTpVouz2BkRjMQ==</Secret2>
         *         </SignInResponse>
         *     </soap:Body>
         * </soap:Envelope>
         */

        String resultBody = result.getBody();

        Map<String, Object> parsedBody = null;
        try {
            parsedBody = parse(resultBody);
        } catch (DocumentException e) {
            return null;
        }

        System.out.println(resultBody);

        Map<String, Object> m = new HashMap<>();
        m.put("thirdType", parsedBody.get("ThirdType"));
        m.put("Secret1", parsedBody.get("Secret1"));
        m.put("Secret2", parsedBody.get("Secret2"));
        System.out.println(parsedBody.get("ThirdType"));
        System.out.println(parsedBody.get("Secret1"));
        System.out.println(parsedBody.get("Secret2"));

        return m;
    }


    /**
     * 收费冲正
     */
    public String AntiPayment(String sidNo,Integer shopID, String money) throws Exception{
        //1帐号、2卡号、3卡内编号、4个人编号、5第三方对接号
        Condition condition = new Condition(ShopZyParam.class);
        condition.or().andEqualTo("shopId",shopID);
        Optional<ShopZyParam> shopZyParamOptional = shopZyParamRepository.findByCondition(condition).stream().findFirst();
        ShopZyParam shopZyParam = shopZyParamOptional.get();

        String nIDType = shopZyParam.getNidtype();
        String nDealerNum = shopZyParam.getNdealernum();//一卡通商户编号
        String nOptNum = shopZyParam.getNoptnum();//一卡通操作员编号
        String nEWalletNum = "1";//主钱包为1，共8个钱包
        String nMonDeal = money;//单位：分
//        String nMonDeal = "150";//单位：分
        String sDealTime = simpleDateFormat.format(new Date());//yyyyMMddHHmmss

        String url = shopZyParam.getZyUrl() + "?op=AntiPayment";

        String request = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                "  <soap12:Header>\n" +
                "    <SecurityHeader xmlns=\"http://www.hzsun.com/\">\n" +
                "      <ThirdType>" + shopZyParam.getThirdType() + "</ThirdType>\n" +
                "      <Secret1>" + shopZyParam.getSecret1() + "</Secret1>\n" +
                "      <Secret2>" + shopZyParam.getSecret2() + "</Secret2>\n" +
                "    </SecurityHeader>\n" +
                "  </soap12:Header>\n" +
                "  <soap12:Body>\n" +
                "    <AntiPayment xmlns=\"http://www.hzsun.com/\">\n" +
                "      <sIDNo>" + sidNo + "</sIDNo>\n" +
                "      <nIDType>" + nIDType + "</nIDType>\n" +
                "      <nStaNum>" + shopZyParam.getStanum() + "</nStaNum>\n" +
                "      <nOptNum>" + nOptNum + "</nOptNum>\n" +
                "      <nDealerNum>" + nDealerNum + "</nDealerNum>\n" +
                "      <nEWalletNum>" + nEWalletNum + "</nEWalletNum>\n" +
                "      <nMonDeal>" + nMonDeal + "</nMonDeal>\n" +
                "      <sDealTime>" + sDealTime + "</sDealTime>\n" +
                "    </AntiPayment>\n" +
                "  </soap12:Body>\n" +
                "</soap12:Envelope>";


        HttpHeaders DEFAULT_JSON_HEADER = new HttpHeaders();
        MediaType jsontype = MediaType.TEXT_XML;
        DEFAULT_JSON_HEADER.setContentType(jsontype);
        DEFAULT_JSON_HEADER.add("Accept", MediaType.TEXT_XML.toString());

        HttpEntity<String> entity = new HttpEntity<>(request, DEFAULT_JSON_HEADER);

        ResponseEntity<String> result = restTemplate.postForEntity(url, entity, String.class);

        /**
         * response
         * <?xml version="1.0" encoding="utf-8"?>
         * <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
         *     <soap:Body>
         *         <AntiPaymentResponse xmlns="http://www.hzsun.com/">
         *             <AntiPaymentResult>00000003</AntiPaymentResult>
         *             <nStaSID>0</nStaSID>
         *         </AntiPaymentResponse>
         *     </soap:Body>
         * </soap:Envelope>
         */

        String resultBody = result.getBody();

        System.out.println(resultBody);
        Map<String, Object> parsedBody = parse(resultBody);

        System.out.println(parsedBody.get("AntiPaymentResult"));
        return (String)parsedBody.get("nStaSID");
    }


    /**
     * 读余额
     */
    public String GetAccDBMoney(String sIDNo, String requesturl){
        //1帐号、2卡号、3卡内编号、4个人编号、5第三方对接号
        String nEWalletNum = "1";//主钱包为1，共8个钱包


        String url = requesturl + "?op=AntiPayment";

        String request = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                "  <soap12:Header>\n" +
                "    <SecurityHeader xmlns=\"http://www.hzsun.com/\">\n" +
                "      <ThirdType>" + 306 + "</ThirdType>\n" +
                "      <Secret1>" + 2067769 + "</Secret1>\n" +
                "      <Secret2>" + "7b1sxwFNuTp6DpMaX944mA==" + "</Secret2>\n" +
                "    </SecurityHeader>\n" +
                "  </soap12:Header>\n" +
                "  <soap12:Body>\n" +
                "    <GetAccDBMoney xmlns=\"http://www.hzsun.com/\">\n" +
                "      <sIDNo>" + sIDNo + "</sIDNo>\n" +
                "      <nIDType>" + 1 + "</nIDType>\n" +
                "      <nEWalletNum>" + nEWalletNum + "</nEWalletNum>\n" +
                "    </GetAccDBMoney>\n" +
                "  </soap12:Body>\n" +
                "</soap12:Envelope>";

        System.out.println("requestBody=\n" + request);

        HttpHeaders DEFAULT_JSON_HEADER = new HttpHeaders();
        MediaType jsontype = MediaType.TEXT_XML;
        DEFAULT_JSON_HEADER.setContentType(jsontype);
        DEFAULT_JSON_HEADER.add("Accept", MediaType.TEXT_XML.toString());

        HttpEntity<String> entity = new HttpEntity<>(request, DEFAULT_JSON_HEADER);

        ResponseEntity<String> result = restTemplate.postForEntity(url, entity, String.class);

        /**
         * response
         * <?xml version="1.0" encoding="utf-8"?>
         *
         * <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
         *   <soap:Body>
         *     <GetAccDBMoneyResponse xmlns="http://www.hzsun.com/">
         *       <GetAccDBMoneyResult>00000001</GetAccDBMoneyResult>
         *       <nMoney>0</nMoney>
         *     </GetAccDBMoneyResponse>
         *   </soap:Body>
         * </soap:Envelope>
         */

        String resultBody = result.getBody();

        System.out.println("resultBody=\n");
        System.out.println(resultBody);

        Map<String, Object> parsedBody = null;
        try {
            parsedBody = parse(resultBody);
        } catch (DocumentException e) {
            e.printStackTrace();
            return null;
        }

        System.out.println(parsedBody.get("GetAccDBMoneyResult"));
        System.out.println(parsedBody.get("nMoney"));

        return (String)parsedBody.get("nMoney");
    }


    /**
     * GetAccInfo
     */
    public Map<String, Object> GetAccInfoSpec(String sIDNo, String requestUrl) throws Exception{
        //1帐号、2卡号、3卡内编号、4个人编号、5第三方对接号
        String nIDType = "1";


        String url = requestUrl + "?op=GetAccInfo";

        String request = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                "  <soap12:Header>\n" +
                "    <SecurityHeader xmlns=\"http://www.hzsun.com/\">\n" +
                "      <ThirdType>" + 306 + "</ThirdType>\n" +
                "      <Secret1>" + 2067764 + "</Secret1>\n" +
                "      <Secret2>" + "7b1sxwFNuTqCGK5cOjme6g==" + "</Secret2>\n" +
                "    </SecurityHeader>\n" +
                "  </soap12:Header>\n" +
                "  <soap12:Body>\n" +
                "    <GetAccInfo xmlns=\"http://www.hzsun.com/\">\n" +
                "      <sIDNo>" + sIDNo + "</sIDNo>\n" +
                "      <nIDType>" + nIDType + "</nIDType>\n" +
                "    </GetAccInfo>\n" +
                "  </soap12:Body>\n" +
                "</soap12:Envelope>";

        System.out.println(request);

        HttpHeaders DEFAULT_JSON_HEADER = new HttpHeaders();
        MediaType jsontype = MediaType.TEXT_XML;
        DEFAULT_JSON_HEADER.setContentType(jsontype);
        DEFAULT_JSON_HEADER.add("Accept", MediaType.TEXT_XML.toString());

        HttpEntity<String> entity = new HttpEntity<>(request, DEFAULT_JSON_HEADER);

        ResponseEntity<String> result = restTemplate.postForEntity(url, entity, String.class);

        /**
         * response
         * <?xml version="1.0" encoding="utf-8"?>
         *
         * <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
         *   <soap:Body>
         *     <GetAccDBMoneyResponse xmlns="http://www.hzsun.com/">
         *       <GetAccDBMoneyResult>00000001</GetAccDBMoneyResult>
         *       <nMoney>0</nMoney>
         *     </GetAccDBMoneyResponse>
         *   </soap:Body>
         * </soap:Envelope>
         */

        String resultBody = result.getBody();

        System.out.println("resultBody=\n");
        System.out.println(resultBody);

        Map<String, Object> parsedBody = parse(resultBody);

        System.out.println(parsedBody.get("Data"));
        String data = parsedBody.get("Data").toString().toUpperCase();
        Integer accnum = data.indexOf("ACCNUM");
        //第一个冒号位置
        Integer colon1 = data.indexOf("'",accnum);
        //第二个冒号位置
        Integer colon2 = data.indexOf("'",colon1+1);
        String cardNo = data.substring(colon1+1,colon2);
        Integer accname = data.indexOf("ACCNAME");
        colon1 = data.indexOf("'",accname);
        colon2 = data.indexOf("'",colon1+1);
        String customerName = data.substring(colon1+1,colon2);
        Integer mobilecode = data.indexOf("MOBILECODE");
        colon1 = data.indexOf("'",mobilecode);
        colon2 = data.indexOf("'",colon1+1);
        String phoneNum = data.substring(colon1+1,colon2);
        Map<String, Object> customerResult = new HashMap<>();
        customerResult.put("cardNo",cardNo);
        customerResult.put("customerName",customerName);
        customerResult.put("mobilePhone",phoneNum);


        System.out.println(cardNo);
        System.out.println(customerName);
        System.out.println(phoneNum);

        if ("应玲美".equals(customerName)) {
            System.out.println("xxxxxx");
        }
        return customerResult;
    }

    /**
     * 收费冲正
     */
    public String AntiPaymentSpec(String sidNo) throws Exception{
        //1帐号、2卡号、3卡内编号、4个人编号、5第三方对接号
        String nEWalletNum = "1";//主钱包为1，共8个钱包
        String nMonDeal = "150";//单位：分
        String sDealTime = simpleDateFormat.format(new Date());//yyyyMMddHHmmss

        String url = "http://265m342s59.qicp.vip/forwardRequestYK?op=AntiPayment";

        String request = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                "  <soap12:Header>\n" +
                "    <SecurityHeader xmlns=\"http://www.hzsun.com/\">\n" +
                "      <ThirdType>" + 306 + "</ThirdType>\n" +
                "      <Secret1>" + 2067748 + "</Secret1>\n" +
                "      <Secret2>" + "7b1sxwFNuToNzjKOoIWOkA==" + "</Secret2>\n" +
                "    </SecurityHeader>\n" +
                "  </soap12:Header>\n" +
                "  <soap12:Body>\n" +
                "    <AntiPayment xmlns=\"http://www.hzsun.com/\">\n" +
                "      <sIDNo>" + sidNo + "</sIDNo>\n" +
                "      <nIDType>" + 1 + "</nIDType>\n" +
                "      <nStaNum>" + 13 + "</nStaNum>\n" +
                "      <nOptNum>" + 1001 + "</nOptNum>\n" +
                "      <nDealerNum>" + 1001 + "</nDealerNum>\n" +
                "      <nEWalletNum>" + nEWalletNum + "</nEWalletNum>\n" +
                "      <nMonDeal>" + nMonDeal + "</nMonDeal>\n" +
                "      <sDealTime>" + sDealTime + "</sDealTime>\n" +
                "    </AntiPayment>\n" +
                "  </soap12:Body>\n" +
                "</soap12:Envelope>";


        HttpHeaders DEFAULT_JSON_HEADER = new HttpHeaders();
        MediaType jsontype = MediaType.TEXT_XML;
        DEFAULT_JSON_HEADER.setContentType(jsontype);
        DEFAULT_JSON_HEADER.add("Accept", MediaType.TEXT_XML.toString());

        HttpEntity<String> entity = new HttpEntity<>(request, DEFAULT_JSON_HEADER);

        ResponseEntity<String> result = restTemplate.postForEntity(url, entity, String.class);

        /**
         * response
         * <?xml version="1.0" encoding="utf-8"?>
         * <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
         *     <soap:Body>
         *         <AntiPaymentResponse xmlns="http://www.hzsun.com/">
         *             <AntiPaymentResult>00000003</AntiPaymentResult>
         *             <nStaSID>0</nStaSID>
         *         </AntiPaymentResponse>
         *     </soap:Body>
         * </soap:Envelope>
         */

        String resultBody = result.getBody();

        System.out.println(resultBody);
        Map<String, Object> parsedBody = parse(resultBody);

        System.out.println(parsedBody.get("AntiPaymentResult"));
        return (String)parsedBody.get("nStaSID");
    }


    /**
     * 商户收费
     */
    public String PaymentAWithStatnum(String sidNo,String orderMoney,Integer shopID, String statnum){

        //1帐号、2卡号、3卡内编号、4个人编号、5第三方对接号
        Condition condition = new Condition(ShopZyParam.class);
        condition.or().andEqualTo("shopId",shopID);
        Optional<ShopZyParam> shopZyParamOptional = shopZyParamRepository.findByCondition(condition).stream().findFirst();
        ShopZyParam shopZyParam = shopZyParamOptional.get();

        if (statnum == null) {
            statnum = shopZyParam.getStanum();
        }

        String nIDType = shopZyParam.getNidtype();
        String nDealerNum = shopZyParam.getNdealernum();//一卡通商户编号
        String nOptNum = shopZyParam.getNoptnum();//一卡通操作员编号
        String nEWalletNum = "1";//主钱包为1，共8个钱包
        String nMonDeal = orderMoney;//单位：分
        String sDealTime = simpleDateFormat.format(new Date());//yyyyMMddHHmmss

        String url = shopZyParam.getZyUrl() + "?op=PaymentA";

        String request = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                "  <soap12:Header>\n" +
                "    <SecurityHeader xmlns=\"http://www.hzsun.com/\">\n" +
                "      <ThirdType>" + shopZyParam.getThirdType() + "</ThirdType>\n" +
                "      <Secret1>" + shopZyParam.getSecret1() + "</Secret1>\n" +
                "      <Secret2>" + shopZyParam.getSecret2() + "</Secret2>\n" +
                "    </SecurityHeader>\n" +
                "  </soap12:Header>\n" +
                "  <soap12:Body>\n" +
                "    <PaymentA xmlns=\"http://www.hzsun.com/\">\n" +
                "      <sIDNo>" + sidNo + "</sIDNo>\n" +
                "      <nIDType>" + nIDType + "</nIDType>\n" +
                "      <nStaNum>" + statnum + "</nStaNum>\n" +
                "      <nOptNum>" + nOptNum + "</nOptNum>\n" +
                "      <nDealerNum>" + nDealerNum + "</nDealerNum>\n" +
                "      <nEWalletNum>" + nEWalletNum + "</nEWalletNum>\n" +
                "      <nMonDeal>" + nMonDeal + "</nMonDeal>\n" +
                "      <sDealTime>" + sDealTime + "</sDealTime>\n" +
                "    </PaymentA>\n" +
                "  </soap12:Body>\n" +
                "</soap12:Envelope>";

        System.out.println(request);

        HttpHeaders DEFAULT_JSON_HEADER = new HttpHeaders();
        MediaType jsontype = MediaType.TEXT_XML;
        DEFAULT_JSON_HEADER.setContentType(jsontype);
        DEFAULT_JSON_HEADER.add("Accept", MediaType.TEXT_XML.toString());

        try {
            HttpEntity<String> entity = new HttpEntity<>(request, DEFAULT_JSON_HEADER);

            ResponseEntity<String> result = restTemplate.postForEntity(url, entity, String.class);

            /**
             * response
             * <?xml version="1.0" encoding="utf-8"?>
             * <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
             *     <soap:Body>
             *         <AntiPaymentResponse xmlns="http://www.hzsun.com/">
             *             <PaymentAResponse>00000003</PaymentAResponse>
             *             <nStaSID>0</nStaSID>
             *         </AntiPaymentResponse>
             *     </soap:Body>
             * </soap:Envelope>
             */

            String resultBody = result.getBody();

            System.out.println(resultBody);
            Map<String, Object> parsedBody = parse(resultBody);
            System.out.println(parsedBody.get("PaymentAResult"));
            System.out.println(parsedBody.get("nStaSID"));

            if (parsedBody.get("nStaSID").toString().equals("0")) {
                return "failed";
            }
            return (String)parsedBody.get("PaymentAResult");
        }catch (Exception e){
            return "failed";
        }
    }
    /**
     * 商户收费
     */
    public String PaymentA(String sidNo,String orderMoney,Integer shopID){
        return PaymentAWithStatnum(sidNo, orderMoney, shopID, null);
    }

    /**
     * 读余额
     */
    public String GetAccDBMoney(String sIDNo,Integer shopID, String idType){
        //1帐号、2卡号、3卡内编号、4个人编号、5第三方对接号
        Condition condition = new Condition(ShopZyParam.class);
        condition.or().andEqualTo("shopId",shopID);
        Optional<ShopZyParam> shopZyParamOptional = shopZyParamRepository.findByCondition(condition).stream().findFirst();
        ShopZyParam shopZyParam = shopZyParamOptional.get();

        String nIDType = shopZyParam.getNidtype();
        String nEWalletNum = "1";//主钱包为1，共8个钱包

        if (idType != null) {
            nIDType = idType;
        }


        String url = shopZyParam.getZyUrl() + "?op=AntiPayment";

        String request = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                "  <soap12:Header>\n" +
                "    <SecurityHeader xmlns=\"http://www.hzsun.com/\">\n" +
                "      <ThirdType>" + shopZyParam.getThirdType() + "</ThirdType>\n" +
                "      <Secret1>" + shopZyParam.getSecret1() + "</Secret1>\n" +
                "      <Secret2>" + shopZyParam.getSecret2() + "</Secret2>\n" +
                "    </SecurityHeader>\n" +
                "  </soap12:Header>\n" +
                "  <soap12:Body>\n" +
                "    <GetAccDBMoney xmlns=\"http://www.hzsun.com/\">\n" +
                "      <sIDNo>" + sIDNo + "</sIDNo>\n" +
                "      <nIDType>" + nIDType + "</nIDType>\n" +
                "      <nEWalletNum>" + nEWalletNum + "</nEWalletNum>\n" +
                "    </GetAccDBMoney>\n" +
                "  </soap12:Body>\n" +
                "</soap12:Envelope>";

        System.out.println("requestBody=\n" + request);

        HttpHeaders DEFAULT_JSON_HEADER = new HttpHeaders();
        MediaType jsontype = MediaType.TEXT_XML;
        DEFAULT_JSON_HEADER.setContentType(jsontype);
        DEFAULT_JSON_HEADER.add("Accept", MediaType.TEXT_XML.toString());

        HttpEntity<String> entity = new HttpEntity<>(request, DEFAULT_JSON_HEADER);

        ResponseEntity<String> result = restTemplate.postForEntity(url, entity, String.class);

        /**
         * response
         * <?xml version="1.0" encoding="utf-8"?>
         *
         * <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
         *   <soap:Body>
         *     <GetAccDBMoneyResponse xmlns="http://www.hzsun.com/">
         *       <GetAccDBMoneyResult>00000001</GetAccDBMoneyResult>
         *       <nMoney>0</nMoney>
         *     </GetAccDBMoneyResponse>
         *   </soap:Body>
         * </soap:Envelope>
         */

        String resultBody = result.getBody();

        System.out.println("resultBody=\n");
        System.out.println(resultBody);

        Map<String, Object> parsedBody = null;
        try {
            parsedBody = parse(resultBody);
        } catch (DocumentException e) {
            e.printStackTrace();
            return null;
        }

        System.out.println(parsedBody.get("GetAccDBMoneyResult"));
        System.out.println(parsedBody.get("nMoney"));

        return (String)parsedBody.get("nMoney");
    }

    public static Map<String, Object> map = new HashMap<>();

    public static Map<String, Object> parse(String soap) throws DocumentException {
        Document doc = DocumentHelper.parseText(soap);//报文转成doc对象
        Element root = doc.getRootElement();//获取根元素，准备递归解析这个XML树
        getCode(root);
        return map;
    }

    public static void getCode(Element root) {
        if (root.elements() != null) {
            List<Element> list = root.elements();//如果当前跟节点有子节点，找到子节点
            for (Element e : list) {//遍历每个节点
                if (e.elements().size() > 0) {
                    getCode(e);//当前节点不为空的话，递归遍历子节点；
                }
                if (e.elements().size() == 0) {
                    map.put(e.getName(), e.getTextTrim());
                }//如果为叶子节点，那么直接把名字和值放入map
            }
        }
    }



    /**
     * GetAccInfo
     */
    public Map<String, Object> GetAccInfo(String sIDNo, Integer shopId) throws Exception{
        Condition condition = new Condition(ShopZyParam.class);
        condition.or().andEqualTo("shopId", shopId);
        Optional<ShopZyParam> shopZyParamOptional = shopZyParamRepository.findByCondition(condition).stream().findFirst();
        ShopZyParam shopZyParam = shopZyParamOptional.get();
        //1帐号、2卡号、3卡内编号、4个人编号、5第三方对接号
        String nIDType = "2";


        String url = shopZyParam.getZyUrl() + "?op=GetAccInfo";

        String request = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                "  <soap12:Header>\n" +
                "    <SecurityHeader xmlns=\"http://www.hzsun.com/\">\n" +
                "      <ThirdType>" + shopZyParam.getThirdType() + "</ThirdType>\n" +
                "      <Secret1>" + shopZyParam.getSecret1() + "</Secret1>\n" +
                "      <Secret2>" + shopZyParam.getSecret2() + "</Secret2>\n" +
                "    </SecurityHeader>\n" +
                "  </soap12:Header>\n" +
                "  <soap12:Body>\n" +
                "    <GetAccInfo xmlns=\"http://www.hzsun.com/\">\n" +
                "      <sIDNo>" + sIDNo + "</sIDNo>\n" +
                "      <nIDType>" + nIDType + "</nIDType>\n" +
                "    </GetAccInfo>\n" +
                "  </soap12:Body>\n" +
                "</soap12:Envelope>";

        System.out.println(request);

        HttpHeaders DEFAULT_JSON_HEADER = new HttpHeaders();
        MediaType jsontype = MediaType.TEXT_XML;
        DEFAULT_JSON_HEADER.setContentType(jsontype);
        DEFAULT_JSON_HEADER.add("Accept", MediaType.TEXT_XML.toString());

        HttpEntity<String> entity = new HttpEntity<>(request, DEFAULT_JSON_HEADER);

        ResponseEntity<String> result = restTemplate.postForEntity(url, entity, String.class);

        /**
         * response
         * <?xml version="1.0" encoding="utf-8"?>
         *
         * <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
         *   <soap:Body>
         *     <GetAccDBMoneyResponse xmlns="http://www.hzsun.com/">
         *       <GetAccDBMoneyResult>00000001</GetAccDBMoneyResult>
         *       <nMoney>0</nMoney>
         *     </GetAccDBMoneyResponse>
         *   </soap:Body>
         * </soap:Envelope>
         */

        String resultBody = result.getBody();

        System.out.println("resultBody=\n");
        System.out.println(resultBody);

        Map<String, Object> parsedBody = parse(resultBody);

        System.out.println(parsedBody.get("Data"));
        String data = parsedBody.get("Data").toString().toUpperCase();
        Integer accnum = data.indexOf("ACCNUM");
        //第一个冒号位置
        Integer colon1 = data.indexOf("'",accnum);
        //第二个冒号位置
        Integer colon2 = data.indexOf("'",colon1+1);
        String cardNo = data.substring(colon1+1,colon2);
        Integer accname = data.indexOf("ACCNAME");
        colon1 = data.indexOf("'",accname);
        colon2 = data.indexOf("'",colon1+1);
        String customerName = data.substring(colon1+1,colon2);
        Integer mobilecode = data.indexOf("MOBILECODE");
        colon1 = data.indexOf("'",mobilecode);
        colon2 = data.indexOf("'",colon1+1);
        String phoneNum = data.substring(colon1+1,colon2);
        Map<String, Object> customerResult = new HashMap<>();
        customerResult.put("cardNo",cardNo);
        customerResult.put("customerName",customerName);
        customerResult.put("mobilePhone",phoneNum);


        System.out.println(cardNo);
        System.out.println(customerName);
        System.out.println(phoneNum);
        return customerResult;
    }

    public static void main2(String[] args) {
        String s = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><GetAccInfoResponse xmlns=\"http://www.hzsun.com/\"><GetAccInfoResult>1</GetAccInfoResult><Data>{Table1:[{AccNum:'672',CARDID:'1213308007',CardCode:'56',AccStatus:'1',AccType:'1',PerCode:'0672',AREANUM:'1',AccName:'施志宇',DEPNUM:'3',DEPNAME:'党政办公室',CLSNUM:'1',CLSNAME:'职工',AccSex:'男',querypwd:'b7C0s1T1VrY=',paypwd:'/SwGZ/q34Ns=',mobileCode:' ',EMail:' ',PostCode:' ',CertCode:' ',CertTypeNum:' ',CertTypeName:' ',ArcClassNum:' ',ArcClassName:' ',NationNum:' ',NationName:' ',PolityNum:' ',PolityName:' ',DutyNum:' ',DutyNum1:' ',TechNum:' ',TechName:' ',PostNum:' ',PostName:' ',EnterTime:' ',BirthDay:' ',PostDate:'2019-04-30',LostDate:'2029-04-25',predeldate:'',deldate:'',authencode:'123359933',remark:'',writeflag:'0',monopen:'0.00',Reserved1:'',Reserved2:'',Reserved3:'',Reserved4:'',Reserved5:'',FixID:'58',appSerialID:'',MonOpen1:'0.00',SourceAccNum:'0',Reserved6:'',Reserved7:'',Reserved8:'',Reserved9:'',Reserved10:'',cardtype:'1',EquipCardDate:'2019/4/30 16:30:13',addr:''}]}</Data></GetAccInfoResponse></soap:Body></soap:Envelope>";

        System.out.println(s);
        String data = s.toUpperCase();
        Integer accnum = data.indexOf("ACCNUM");
        //第一个冒号位置
        Integer colon1 = data.indexOf("'",accnum);
        //第二个冒号位置
        Integer colon2 = data.indexOf("'",colon1+1);
        String cardNo = data.substring(colon1+1,colon2);
        Integer accname = data.indexOf("ACCNAME");
        colon1 = data.indexOf("'",accname);
        colon2 = data.indexOf("'",colon1+1);
        String customerName = data.substring(colon1+1,colon2);
        Integer mobilecode = data.indexOf("MOBILECODE");
        colon1 = data.indexOf("'",mobilecode);
        colon2 = data.indexOf("'",colon1+1);
        String phoneNum = data.substring(colon1+1,colon2);
        Map<String, Object> customerResult = new HashMap<>();
        customerResult.put("cardNo",cardNo);
        customerResult.put("customerName",customerName);
        customerResult.put("mobilePhone",phoneNum);


        System.out.println(cardNo);
        System.out.println(customerName);
        System.out.println(phoneNum);


        Customer customer = new Customer();
        customer.setName("施志宇");

        customer.setPhone("12312");
        if (customer.getPhone() != null && customer.getPhone().equals(phoneNum)) {
            System.out.println("用户手机号匹配:" + phoneNum);
        } else {
            if (customer.getName() != null && customer.getName().equals(customerName)
                    && (StringUtils.isEmpty(phoneNum) || StringUtils.isEmpty(phoneNum.trim()))
                    ) {
                System.out.println("用户姓名匹配:" + customerName);
            } else {
                System.out.println("用户不匹配");
                return;
            }
        }

    }


    /**
     * GetAccInfo
     */
    public Map<String, Object> TestGetAccInfo(String sIDNo, Integer shopId) throws Exception{
        ShopZyParam shopZyParam = new ShopZyParam();
        shopZyParam.setZyUrl("http://m24695a789.zicp.vip/ThirdWebservice.asmx");
        shopZyParam.setThirdType("306");
        shopZyParam.setSecret1("2067683");
        shopZyParam.setSecret2("7b1sxwFNuTrLkVIn1Fq7UA==");
        shopZyParam.setStanum("63");
        shopZyParam.setNidtype("2");


        //1帐号、2卡号、3卡内编号、4个人编号、5第三方对接号
        String nIDType = "2";


        String url = shopZyParam.getZyUrl() + "?op=GetAccInfo";

        String request = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                "  <soap12:Header>\n" +
                "    <SecurityHeader xmlns=\"http://www.hzsun.com/\">\n" +
                "      <ThirdType>" + shopZyParam.getThirdType() + "</ThirdType>\n" +
                "      <Secret1>" + shopZyParam.getSecret1() + "</Secret1>\n" +
                "      <Secret2>" + shopZyParam.getSecret2() + "</Secret2>\n" +
                "    </SecurityHeader>\n" +
                "  </soap12:Header>\n" +
                "  <soap12:Body>\n" +
                "    <GetAccInfo xmlns=\"http://www.hzsun.com/\">\n" +
                "      <sIDNo>" + sIDNo + "</sIDNo>\n" +
                "      <nIDType>" + nIDType + "</nIDType>\n" +
                "    </GetAccInfo>\n" +
                "  </soap12:Body>\n" +
                "</soap12:Envelope>";


        HttpHeaders DEFAULT_JSON_HEADER = new HttpHeaders();
        MediaType jsontype = MediaType.TEXT_XML;
        DEFAULT_JSON_HEADER.setContentType(jsontype);
        DEFAULT_JSON_HEADER.add("Accept", MediaType.TEXT_XML.toString());

        HttpEntity<String> entity = new HttpEntity<>(request, DEFAULT_JSON_HEADER);

        ResponseEntity<String> result = restTemplate.postForEntity(url, entity, String.class);

        /**
         * response
         * <?xml version="1.0" encoding="utf-8"?>
         *
         * <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
         *   <soap:Body>
         *     <GetAccDBMoneyResponse xmlns="http://www.hzsun.com/">
         *       <GetAccDBMoneyResult>00000001</GetAccDBMoneyResult>
         *       <nMoney>0</nMoney>
         *     </GetAccDBMoneyResponse>
         *   </soap:Body>
         * </soap:Envelope>
         */

        String resultBody = result.getBody();

        System.out.println("resultBody=\n");
        System.out.println(resultBody);

        Map<String, Object> parsedBody = parse(resultBody);

        System.out.println(parsedBody.get("Data"));
        String data = parsedBody.get("Data").toString();
        Integer accnum = data.indexOf("ACCNUM");
        //第一个冒号位置
        Integer colon1 = data.indexOf("'",accnum);
        //第二个冒号位置
        Integer colon2 = data.indexOf("'",colon1+1);
        String cardNo = data.substring(colon1+1,colon2);
        Integer accname = data.indexOf("ACCNAME");
        colon1 = data.indexOf("'",accname);
        colon2 = data.indexOf("'",colon1+1);
        String customerName = data.substring(colon1+1,colon2);
        Map<String, Object> customerResult = new HashMap<>();
        customerResult.put("cardNo",cardNo);
        customerResult.put("customerName",customerName);
        return customerResult;
    }
}
